<?php
namespace Admin\Model;
use Think\Model\RelationModel;

class AcademyModel extends RelationModel {

	protected $_validate = array(
		array('id', 'require', 'id不能为空！'),
		array('name', 'require', '名称不能为空！'),
		array('name', '', '学院已存在！', 1, 'unique', 3),
	);

	protected $_auto = array(
		// array(完成字段1,完成规则,[完成条件,附加规则]),
		array('id', 'intval', 2, 'function'),
	);

	protected $_link = array(
		'majors'	=> array(
		    'mapping_type'  => self::HAS_MANY,
		    'class_name'    => 'Major',
		    'foreign_key'   => 'academy_id',
		),
		'teachers'	=> array(
		    'mapping_type'  => self::HAS_MANY,
		    'class_name'    => 'Teacher',
		    'foreign_key'   => 'academy_id',
		),
	);

	public function hasChildObj($id) {
		$result = $this->where(array('id' => $id))->relation('majors')->find();
		if($result['majors']) {
			$this->error = '该学院下面还存在专业未删除，请先删除这些专业！';
			return true;
		}
		$result = array();
		$result = $this->where(array('id' => $id))->relation('teachers')->find();
		if($result['teachers']) {
			$this->error = '该学院下面还存在教师未删除，请先删除这些教师！';
			return true;
		}
		return false;
	}

}
